﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ESpace_Project
{
    
    public partial class FormSortList : Form
    {
        public FormSortList()
        {
            InitializeComponent();
            Hide();
        }

        List<User> ul = null;
        User[] um = null;

        void ReFill()
        {
            lb.Items.Clear();
            if (ul == null && um == null) return;

            User[] u;

            if (ul == null) u = um;
            else u = ul.ToArray();

            for (int i = 0; i < u.Length; i++)
                lb.Items.Add(u[i].name + " >> " + u[i].age.ToString());

        }
        
        Random rnd = new Random();
        String GetRndName()
        {
            string ret = "";
            int l = rnd.Next(5) + 3;
            for (int i = 0; i < l; i++)
                ret += ((char)(rnd.Next(26) + 65)).ToString();
            return ret;
        }

        int CompareUName(User u1, User u2)
        {
            return ((User)u1).name.CompareTo(((User)u2).name);
        }

        int CompareUAge(User u1, User u2)
        {
            return ((User)u1).age.CompareTo(((User)u2).age);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            um = null;
            ul = new List<User>();

            for (int i = 0; i < 10; i++)
            { 
                ul.Add(new User(GetRndName(),rnd.Next(20)+10));
            }
            ReFill();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ul = null;
            um = new User[10];

            for (int i = 0; i < 10; i++)
            {
                um[i] = new User(GetRndName(), rnd.Next(20) + 10);
            }
            ReFill();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ul.Sort(delegate(User obj1, User obj2) { return CompareUName(obj1, obj2); });
            ReFill();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ul.Sort(delegate(User obj1, User obj2) { return CompareUAge(obj1, obj2); });
            ReFill();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Array.Sort(um, delegate(User obj1, User obj2) { return CompareUName(obj1, obj2); });

            ReFill();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Array.Sort(um, delegate(User obj1, User obj2) { return CompareUAge(obj1, obj2); });
            ReFill();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            //Hide();
        }

        
    }
}
